package it.unisa.ingegneria.tdp.esercitazione4;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
/**
 * 
 * Classe di test per la classe Btree
 *
 */
public class BTreeTest {

	static final int MAX = 3;
	
	BTree <Integer,String> bTreeInt;
	
	BTree <String,String> bTreeStr;
	
	@Before
	public void setUp(){
	
		bTreeInt=new BTree<Integer, String>(MAX);
		
	}
	
	
	
	@Test
	public void testIsEmpty(){		
	
		assertTrue(bTreeInt.isEmpty());
		
		bTreeInt.insert(1, "Info 1");
		
		assertFalse(bTreeInt.isEmpty());
		
		bTreeInt.delete(1);
		
		assertTrue(bTreeInt.isEmpty());
	
	}
	
	@Test
	public void testSearchBTreeEmpty(){
	
		assertNotEquals("Info 1",bTreeInt.search(1));
		
		assertNull(bTreeInt.search(1));
		
	}
	
	@Test
	public void testSearchOneElement(){
	
		assertNotEquals("Info 1",bTreeInt.search(1));
		
		bTreeInt.insert(1, "Info 1");
		
		assertEquals("Info 1",bTreeInt.search(1));
		
		bTreeInt.delete(1);
		
		assertNotEquals("Info 1",bTreeInt.search(1));
	}
	
	@Test
	public void testSearchMoreElement(){
		
		for(int i=0;i<20;i++)
		
			bTreeInt.insert(i,"Info "+i);
		
		assertEquals("Info 1",bTreeInt.search(1));
	}
	
	
	
	
	
	@Test
	public void testDeleteOnElement(){
	
		bTreeInt.insert(1, "Info 1");
		
		bTreeInt.delete(1);
		
		assertNull(bTreeInt.search(1));
	
	}
	
	@Test
	public void testDeleteMoreElement(){
		
		for(int i=0;i<20;i++)
	
			bTreeInt.insert(i,"Info "+i);
		
		bTreeInt.delete(5);
		
		assertNull(bTreeInt.search(5));
	}
	
	
	
	@Test
	public void testInsertMinMax() {
		
		for(int i=0;i<9;i++)
		{
		
			bTreeInt.insert(i,"Info "+i);
		}
				
		for(int i=0;i<9;i++)
		{
		
			assertEquals("Info "+i,bTreeInt.search(i));			
	
		}
		
	
	}

	@Test
	public void testInsertMaxMin() {
		
		for(int i=9;i>0;i--)
		{
		
			bTreeInt.insert(i,"Info "+i);
		
		}
				
		for(int i=1;i<9;i++)
		{
		
			assertEquals("Info "+i,bTreeInt.search(i));			
		
		}
	
	}
	
	@Test
	public void testInsertEq() {
		
		for(int i=1;i<9;i++)
		{
		
			bTreeInt.insert(7,"Info 7");
		
		}	

		assertEquals("Info 7",bTreeInt.search(7));		
		
	}
	
	@Test
	public void testSortKey(){
		
		for(int i=1;i<9;i++)
		
			bTreeInt.insert((int)(Math.random()*10),"Info "+(int)(Math.random()*10));
		
		assertEquals(true, bTreeInt.isOrder());
		
	}
	
	
	
}
